﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command;
using MediatR;

namespace AuthCenter_HuangJingtao.Read.Api.Application.CommandHandler
{
    public class SeAppCommandHandler : IRequestHandler<SeAppCommand, ApiResult<List<ApiTree>>>
    {
        IBaseReponsitory<App> Apps;

        public SeAppCommandHandler(IBaseReponsitory<App> Apps)
        {
            this.Apps = Apps;
        }

        public async Task<ApiResult<List<ApiTree>>> Handle(SeAppCommand request, CancellationToken cancellationToken)
        {
            var list = Apps.GetQuery().Where(c => c.Group_Id == request.Group_Id).ToList();
            List<ApiTree> trees = new List<ApiTree>();
            foreach (var item in list)
            {
                ApiTree tree = new ApiTree();
                tree.id = item.App_Id;
                tree.label = item.App_Name;
                trees.Add(tree);
            }
            return new ApiResult<List<ApiTree>>
            {
                Code = ApiEnum.Success,
                Message = "查询成功",
                Data = trees
            };
        }
    }
}
